﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_WareHouse_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As Warehouse = (From q In data.Warehouses Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                txtBatchno.Text = IIf(IsNothing(p.BatchNo) = True, "", p.BatchNo)
                hidBatchNo.Value = p.BatchNo
                txtItemcode.Text = IIf(IsNothing(p.ItemCode) = True, "", p.ItemCode)

                txtVendorcode.Text = IIf(IsNothing(p.VendorCode) = True, "", p.VendorCode)


                If Not p.ManufacturedDate Is Nothing Then
                    txtNgaysanxuat.Text = CType(p.ManufacturedDate, Date).ToString("dd/MM/yyyy")
                End If

                If Not p.ExpiredDate Is Nothing Then
                    Dim dtNgayHetHan As Date = CType(p.ExpiredDate, Date)
                    If dtNgayHetHan.ToString("dd/MM/yyyy") <> "01/01/1900" Then
                        txtNgayhethan.Text = dtNgayHetHan.ToString("dd/MM/yyyy")
                    Else
                        txtNgayhethan.Text = ""
                    End If
                End If


                txtKhoiluongthetich.Text = IIf(IsNothing(p.PackingSize) = True, "", p.PackingSize)
                txtQuycachdonggoi.Text = IIf(IsNothing(p.PackingType) = True, "", p.PackingType)
                txtSource.Text = IIf(IsNothing(p.Source) = True, "", p.Source)
                txtPallet.Text = IIf(IsNothing(p.Pallet) = True, "", p.Pallet)
                txtStockOnHand.Text = IIf(IsNothing(p.StockOnHand) = True, "", p.StockOnHand)
                txtBUN.Text = IIf(IsNothing(p.BUN) = True, "", p.BUN)

            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Function checkDublicate() As Boolean
        Using data As New VSEntities
            Dim p = (From q In data.WareHouses Where q.BatchNo = txtBatchno.Text.Trim And q.BatchNo <> hidBatchNo.Value Select q).FirstOrDefault
            If Not p Is Nothing Then
                Return True
            End If

        End Using
        Return False
    End Function
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'check trung BatchNO
        If checkDublicate() Then
            Excute_Javascript("Alertbox('Batch No. đã tồn tại trong hệ thống.');", Me.Page, True)
            Exit Sub
        End If
        If txtNgayhethan.Text <> "" AndAlso StringToDate(txtNgaysanxuat.Text.Trim()) >= StringToDate(txtNgayhethan.Text.Trim()) Then
            Excute_Javascript("Alertbox('Ngày Sản xuất phải trước ngày hết hạn.');", Me.Page, True)
            Exit Sub
        End If
        Using data As New VSEntities
            Dim p As WareHouse = (From q In data.WareHouses Where q.Id = hidID.Value).SingleOrDefault
            Try
                p.Id = hidID.Value

                p.BatchNo = txtBatchno.Text.Trim()
                p.ItemCode = txtItemcode.Text.Trim()
                'p.ItemName = txtItemname.Text.Trim()
                p.VendorCode = txtVendorcode.Text.Trim()
                ' p.VendorName = txtVendorname.Text.Trim()
                p.ManufacturedDate = StringToDate(txtNgaysanxuat.Text.Trim())
                p.ExpiredDate = StringToDate(txtNgayhethan.Text.Trim())
                p.PackingSize = txtKhoiluongthetich.Text.Trim()
                p.PackingType = txtQuycachdonggoi.Text.Trim()


                p.Source = txtSource.Text.Trim()
                p.Pallet = txtPallet.Text.Trim()
                p.BUN = txtBUN.Text.Trim()
                p.StockOnHand = txtStockOnHand.Text()

                data.SaveChanges()
                Insert_App_Log("update Warehouse:" & p.Id & " - " & p.BatchNo.ToString, Function_Name.WareHouse, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Warehouse/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ShowData()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
